<script setup lang="ts">
import { createMedicalOrder, MedicalOrderPre, orderAddress } from '@/apis/order';
import type { AddressItem, OrderPre } from '@/types/order';
import { showToast } from 'vant';
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import MedicalOrderDetail from './components/MedicalOrderDetail.vue';
defineOptions({
  name: 'OrderPay'
})
//药品订单响应参数响应式数据
const medicalOrder = ref<OrderPre>()
const route = useRoute()
//药品订单预支付信息
const medicalOrderPay = async () => {
  const res = await MedicalOrderPre({ prescriptionId: route.query.id as string })
  medicalOrder.value = res.data
}

//收获地址响应数据
const address = ref<AddressItem>()
//订单-查询收货地址
const getOrderAddress = async () => {
  const res = await orderAddress()
  //如果有收货地址
  if (res.data.length) {
    const defaultAddress = res.data.find(item => item.isDefault === 1)
    if (defaultAddress) {
      //有默认收货地址
      address.value = defaultAddress
    } else {
      //不是默认地址设置数组中第一个元素
      address.value = res.data[0]
    }
  }
}
onMounted(() => {
  medicalOrderPay()
  getOrderAddress()
})
//勾选我同意
const isAgree = ref(false)
const loading = ref(false)
const orderId = ref('')
const show = ref(false)
//立即支付生成药品订单
const onSubmit = async () => {
  if (!isAgree.value) return showToast('请勾选我同意')
  if (!address.value?.id) return showToast('请选择收获地址')
  if (!medicalOrder.value?.id) return showToast('没有药品订单Id')
  //如果没有订单id
  if (!orderId.value) {
    try {
      loading.value = true
      //发送请求生成药品订单
      const res = await createMedicalOrder({
        id: medicalOrder.value.id,
        addressId: address.value.id,
        couponId: medicalOrder.value.couponId
      })
      orderId.value = res.data.id
      //开启抽屉
      show.value = true
      //关闭loding
      loading.value = false
    } catch (error) {
      //关闭loading
      loading.value = false
    }
  } else {
    //打开抽屉组件
    show.value = true
  }
}
</script>

<template>
  <div class="order-pay-page" v-if="address && medicalOrder">
    <cp-nav-bar title="药品支付" />
    <div class="order-address">
      <p class="area">
        <van-icon name="location" />
        <span>{{ address.province + address.city + address.county }}</span>
      </p>
      <p class="detail">{{ address.addressDetail }}</p>
      <p>{{ address.receiver }} {{ address.mobile.replace(/^(\d{3})\d+(\d{4})$/, '\$1****\$2') }}</p>
    </div>
    <MedicalOrderDetail :medicines="medicalOrder.medicines"></MedicalOrderDetail>
    <div class="order-detail">
      <van-cell-group>
        <van-cell title="药品金额" :value="`￥${medicalOrder.payment}`" />
        <van-cell title="运费" :value="`￥${medicalOrder.expressFee}`" />
        <van-cell title="优惠券" :value="`-￥${medicalOrder.couponDeduction}`" />
        <van-cell title="实付款" :value="`￥${medicalOrder.actualPayment}`" class="price" />
      </van-cell-group>
    </div>
    <div class="order-tip">
      <p class="tip">
        由于药品的特殊性，如非错发、漏发药品的情况，药品一经发出
        不得退换，请核对药品信息无误后下单。
      </p>
      <van-checkbox v-model="isAgree">我已同意<a href="javascript:;">支付协议</a></van-checkbox>
    </div>
    <van-submit-bar :price="medicalOrder.actualPayment * 100" button-text="立即支付" button-type="primary" text-align="left"
      @click="onSubmit"></van-submit-bar>
    <cp-pay-sheet :loading="loading" :actual-payment="medicalOrder.actualPayment" v-model:show="show"
      :order-id="orderId" pay-callback="/order/pay/result"></cp-pay-sheet>
  </div>
  <div class="order-pay-page" v-else>
    <cp-nav-bar title="药品支付" />
    <van-skeleton title avatar :row="2" style="margin-top: 15px;" />
    <van-skeleton title :row="4" style="margin-top: 30px;" />
    <van-skeleton title :row="4" style="margin-top: 30px;" />
  </div>
</template>

<style lang="scss" scoped>
:deep(.van-nav-bar) {
  background-color: var(--cp-primary);

  .van-nav-bar__arrow,
  .van-nav-bar__title {
    color: #fff;
  }
}

:deep(.van-cell) {
  .van-cell__title {
    font-size: 16px;
  }

  .van-cell__value {
    font-size: 16px;
  }

  &.price {
    .van-cell__value {
      font-size: 18px;
      color: var(--cp-price);
    }
  }
}

:deep(.van-submit-bar) {
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);

  .van-button {
    width: 200px;
  }
}

.order-pay-page {
  padding: 46px 0 65px;
}

.order-address {
  padding: 15px 15px 0 15px;
  background-color: #fff;
  font-size: 13px;

  .area {
    color: var(--cp-tag);
    margin-bottom: 5px;

    .van-icon-location {
      color: #ff7702;
      font-size: 14px;
    }
  }

  .detail {
    font-size: 17px;
    margin-bottom: 5px;
  }

  &::after {
    content: '';
    display: block;
    height: 12px;
    background-color: var(--cp-bg);
    margin: 0 -15px;
    margin-top: 15px;
  }
}

.order-medical {
  background-color: #fff;
  padding: 0 15px;

  .head {
    display: flex;
    height: 54px;
    align-items: center;

    >h3 {
      font-size: 16px;
      font-weight: normal;
    }

    >small {
      font-size: 13px;
      color: var(--cp-tag);
      margin-left: 10px;
    }
  }

  .item {
    display: flex;
    flex-wrap: wrap;
    padding: 15px 0;

    .img {
      width: 80px;
      height: 70px;
      border-radius: 2px;
      overflow: hidden;
    }

    .info {
      padding-left: 15px;
      width: 250px;

      .name {
        display: flex;
        font-size: 15px;
        margin-bottom: 5px;

        >span:first-child {
          width: 200px;
        }

        >span:last-child {
          width: 50px;
          text-align: right;
        }
      }

      .size {
        margin-bottom: 5px;

        .van-tag {
          background-color: var(--cp-primary);
          vertical-align: middle;
        }

        span:not(.van-tag) {
          margin-left: 10px;
          color: var(--cp-tag);
          vertical-align: middle;
        }
      }

      .price {
        font-size: 16px;
        color: #eb5757;
      }
    }

    .desc {
      width: 100%;
      background-color: var(--cp-bg);
      border-radius: 4px;
      margin-top: 10px;
      padding: 4px 10px;
      color: var(--cp-tip);
    }
  }
}

.order-tip {
  padding: 0 15px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 10px;

  .tip {
    font-size: 12px;
    color: var(--cp-tag);
    width: 100%;

    &::before {
      content: '*';
      color: var(--cp-price);
      font-size: 14px;
    }

    margin-bottom: 30px;
  }

  .van-checkbox {
    a {
      color: var(--cp-primary);
    }
  }
}
</style>
